package com.sun.controller.pms;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.common.MsResult;
import com.sun.po.*;
import com.sun.pojo.*;
import com.sun.service.*;
import com.sun.service.pms.IPmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author 妙妙屋
 * @since 2021-12-15
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {
    @Autowired
    private IPmsProductService pmsProductService;

    @Autowired
    private IPmsMemberPriceService pmsMemberPriceService;

    @Autowired
    private IPmsProductAttributeValueService productAttributeValueService;

    @Autowired
    private IPmsProductFullReductionService productFullReductionService;

    @Autowired
    private IPmsProductLadderService productLadderService;

    @Autowired
    private IPmsSkuStockService skuStockService;

    @Autowired
    private ICmsSubjectService subjectService;

    @Autowired
    private ICmsPrefrenceAreaService prefrenceAreaService;


    @GetMapping("/list")
    public MsResult list(Long pageSize,Long pageNum,String keyword,PmsProduct pmsProduct){
        IPage page = pmsProductService.selectAll(pageSize,pageNum,keyword,pmsProduct);
        return MsResult.isOkDataPage(page);
    }

    @PostMapping("/create")
    public MsResult crate(@RequestBody PmsProductPo productPo){
        //插入到pms_product表
        pmsProductService.save(productPo);

        //获取productId
        Long productId = productPo.getId();

        //插入到pms_member_price
        for (PmsMemberPrice pmsMemberPrice : productPo.getMemberPriceList()) {
            pmsMemberPrice.setProductId(productId);
            pmsMemberPriceService.save(pmsMemberPrice);
        }

        //插入pms_product_attribute_value
        for (PmsProductAttributeValue pmsProductAttributeValue : productPo.getProductAttributeValueList()) {
            pmsProductAttributeValue.setProductId(productId);
            productAttributeValueService.save(pmsProductAttributeValue);
        }
        //插入pms_product_full_reduction
        for (PmsProductFullReduction o : productPo.getProductFullReductionList()) {
            o.setProductId(productId);
            productFullReductionService.save(o);
        }
        //插入pms_product_ladder
        for (PmsProductLadder o : productPo.getProductLadderList()) {
            o.setProductId(productId);
            productLadderService.save(o);
        }

        //插入pms_sku_stock
        for (PmsSkuStock o : productPo.getSkuStockList()) {
            o.setProductId(productId);
            skuStockService.save(o);
        }

        //插入cms_subject_product_relation
        for (SubjectProductRelation o : productPo.getSubjectProductRelationList()) {
            o.setProductId(productId);
            subjectService.saveRelation(o);
        }

        //插入cms_preference_area_product_relation
        for (PrefrenceAreaProductRelation o : productPo.getPrefrenceAreaProductRelationList()) {
            o.setProductId(productId);
            prefrenceAreaService.saveRelation(o);
        }

        return MsResult.isOkMessage("创建成功");
    }
}

